package com.myy.auth.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myy.auth.entity.Role;
import com.myy.auth.entity.RoleMenu;
import com.myy.auth.entity.RoleSystem;
import com.myy.auth.mapper.RoleMapper;
import com.myy.auth.vo.RoleVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author ADMIN
* @description 针对表【sys_role(角色表)】的数据库操作Service
* @createDate 2025-08-16 23:40:57
*/
@AllArgsConstructor
@Service
public class RoleService extends ServiceImpl<RoleMapper, Role> {
    private final RoleSystemService roleSystemService;
    private final RoleMenuService roleMenuService;


    /**
     * 创建角色
     * @param roleVo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public String createRole(RoleVo roleVo) {
        valib(roleVo);
        Boolean isUpdate = StrUtil.isNotBlank(roleVo.getId());
        // 1. 保存角色
        Role role = Boolean.TRUE.equals(isUpdate) ? this.getById(roleVo.getId()) : new Role();
        role.setRoleName(roleVo.getRoleName());
        role.setRoleCode(roleVo.getRoleCode());
        role.setRoleSort(roleVo.getRoleSort());
        role.setStatus(roleVo.getStatus());
        role.setRemark(roleVo.getRemark());
        if(isUpdate){
            role.initByUpdate();
        } else {
            role.setId(IdUtil.fastSimpleUUID());
            role.init();
        }
        this.getBaseMapper().insert(role);
        // 2. 批量保存角色-菜单
        if(isUpdate){
            roleMenuService.remove(new LambdaQueryWrapper<RoleMenu>()
                    .eq(RoleMenu::getRoleId, roleVo.getId())
            );
        }
        if (CollUtil.isNotEmpty(roleVo.getMenuIds())) {
            List<RoleMenu> roleMenus = roleVo.getMenuIds().stream()
                    .map(menuId -> {
                        RoleMenu rm = new RoleMenu();
                        rm.setRoleId(role.getId());
                        rm.setMenuId(menuId);
                        return rm;
                    }).collect(Collectors.toList());
            roleMenuService.saveBatch(roleMenus);
        }
        // 3. 批量保存角色-系统
        if(isUpdate){
            roleSystemService.remove(new LambdaQueryWrapper<RoleSystem>()
                    .eq(RoleSystem::getRoleId, roleVo.getId())
            );
        }
        if (CollUtil.isNotEmpty(roleVo.getSystemIds())) {
            List<RoleSystem> roleSystems = roleVo.getSystemIds().stream()
                    .map(sysId -> {
                        RoleSystem rs = new RoleSystem();
                        rs.setRoleId(role.getId());
                        rs.setSystemId(sysId);
                        return rs;
                    }).collect(Collectors.toList());
            roleSystemService.saveBatch(roleSystems);
        }
        return role.getId();
    }

    //有效参数
    public void valib(RoleVo roleVo){
        Assert.notNull(roleVo,"参数不能为空");
        if(StrUtil.isNotBlank(roleVo.getId())){
            Assert.notNull(this.getById(roleVo.getId()),"无效Id");
        }
        Boolean exist = this.lambdaQuery()
                .eq(Role::getRoleCode, roleVo.getRoleCode())
                .ne(StrUtil.isNotBlank(roleVo.getId()), Role::getId, roleVo.getId())
                .exists();
        Assert.isTrue(!exist,"角色编码已存在");
    }
}
